/*
 * COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2017年7月6日    Li.Shangzhi         Create the class
 * http://www.jimilab.com/
 */

package com.jimi.samples.pay.api.impl;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import net.sf.oval.constraint.NotBlank;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import com.jimi.dto.base.PageModel;
import com.jimi.framework.annotation.log.LoggerProfile;
import com.jimi.framework.bean.BeanUtil;
import com.jimi.framework.utils.RowBoundUtils;
import com.jimi.samples.pay.api.IPayTokenApi;
import com.jimi.samples.pay.dto.input.PayTokenInputDto;
import com.jimi.samples.pay.dto.input.PayTokenPageInputDto;
import com.jimi.samples.pay.dto.output.PayTokenOutputDto;
import com.jimi.samples.pay.model.PayTokenEntity;
import com.jimi.samples.pay.service.IPayTokenService;

/**
 * @FileName PayTokenApiImpl.java
 * @Description:
 *
 * @Date 2017年7月6日 下午2:24:49
 * @author Li.Shangzhi
 * @version 1.0
 */
@Service
public class PayTokenApiImpl implements IPayTokenApi {

	@Resource
	IPayTokenService payTokenService;
	

	@Override
	@LoggerProfile(methodNote = "IPayTokenApi.removeById")
	public int removeById(String id) {
		return payTokenService.remove(id);
	}

	@Override
	@LoggerProfile(methodNote = "IPayTokenApi.insert")
	public String insert(@NotBlank.List(value = { @NotBlank(target = "tradeNo"), @NotBlank(target = "tradeType"), @NotBlank(target = "userId"),
			@NotBlank(target = "productDetail") }) PayTokenInputDto payTokenDto) {
		String id = payTokenService.insert(this.dtoToEntity(payTokenDto));
		return id;
	}

	@Override
	@LoggerProfile(methodNote = "IPayTokenApi.findPage")
	public PageModel<PayTokenOutputDto> findPage(PayTokenPageInputDto pageInputDto, PageModel pageModel) {
		PayTokenEntity payTokenEntity = (PayTokenEntity) BeanUtil.convertBean(pageInputDto, PayTokenEntity.class);
		RowBounds rowBounds = RowBoundUtils.rowBounds(pageModel);
		payTokenEntity.setOrderSort(pageModel.getOrderSort());
		payTokenEntity.setOrder(pageModel.getOrder().getValue());

		int totalCount = payTokenService.findPageCount(payTokenEntity);
		List<PayTokenEntity> lstPayTokens = payTokenService.findPage(payTokenEntity, rowBounds);

		return new PageModel<PayTokenOutputDto>(pageModel.getPage(), pageModel.getLimit(), totalCount,
				(List<PayTokenOutputDto>) BeanUtil.convertBeanList(lstPayTokens, PayTokenOutputDto.class));
	}

	@Override
	@LoggerProfile(methodNote = "IPayTokenApi.update")
	public int update(@NotBlank.List(value = { @NotBlank(target = "id") }) PayTokenInputDto payTokenDto) {
		return payTokenService.update(this.dtoToEntity(payTokenDto));
	}

	@Override
	@LoggerProfile(methodNote = "IPayTokenApi.getById")
	public PayTokenOutputDto getById(@NotBlank String id) {
		PayTokenEntity payTokenEntity = payTokenService.getById(id);
		return this.entityToDto(payTokenEntity);
	}

	@Override
	public PayTokenOutputDto getNewPayInfo(String userId) {
		PayTokenEntity payTokenEntity = payTokenService.getNewPayInfo(userId);
		return this.entityToDto(payTokenEntity);
	}
	
	private PayTokenEntity dtoToEntity(Object obj) {
		return (PayTokenEntity) BeanUtil.convertBean(obj, PayTokenEntity.class);
	}

	private PayTokenOutputDto entityToDto(Object obj) {
		return (PayTokenOutputDto) BeanUtil.convertBean(obj, PayTokenOutputDto.class);
	}

}
